<?php
/**
 * Created by PhpStorm.
 * User: alex
 * Date: 25.10.14
 * Time: 22:31
 */

class CMS_SQL {

    public $db;
    public $count_query = 0;
    public $errors = array();

    public function __construct(){
        $this->db = new PDO('mysql:host=localhost;dbname=vikno', "root", "111");
    }

    public function CMS_query($sql, $execute_array, $fetch = PDO::FETCH_ASSOC){
        $q = $this->db->prepare($sql, array( PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY ));
        if ($q){
            $q->execute($execute_array);
            $q->setFetchMode($fetch);
            $r = $q->fetchAll();
            $this->count_query += sizeof($r);
            $this->check_errors();

            return $r;
        }
        return false;
    }

    /**
     * insert/update
     * @param $sql
     * @param $execute_array
     * @return int|bool - count affected rows
     */
    public function CMS_IU($sql, $execute_array){
        $q = $this->db->prepare($sql);
        if ($q){
            $q->setFetchMode(PDO::FETCH_ASSOC);
            $q->execute($execute_array);
            $r = $q->rowCount();

            $this->count_query++;
            $this->check_errors();

            return $r;
        }
        return false;
    }

    private function check_errors(){
        if (strlen(mysql_error())){
            $this->errors[] = mysql_error();
        }
    }

}